<template>
	<view class="page">
		<u-navbar title="摄像头" border @leftClick="$utils.back()" placeholder>
		</u-navbar>
		<view style="flex: 1;">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<view class="BodyBox" v-if="!loading">
					<view class="bodyItem" v-for="(item,index) in data" :key="item">
						<view class="bodyHeader">
							<view class="icon-shexiangtou iconfont font40"></view>
							<view>
								<u-text lines='1' :text='`序列号${item.serial_number}`' size='24rpx' />
							</view>
							<u-icon name="more-dot-fill" size="40rpx" @click='openSelect(item)'></u-icon>
						</view>
						<view>
							<u-text lines='1' :text='item.name?item.name:"请编辑设备名称"' size='24rpx' color='#bbb' />
						</view>
					</view>
				</view>
				<view class="BodyBox" v-else>
					<view class="bodyItem Skeleton" v-for="(item,index) in 20" :key="item" />
				</view>
			</z-paging>
		</view>
		<view class="add" @click="jumpAdd" v-if="!loading">
			<u-icon color='#fff' name='plus' size='50rpx' />
		</view>
		<root-portal>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" content='是否确认删除？' showCancelButton @confirm='del(selectItem.id)'
				@cancel='delShow=false'></u-modal>
			<u-modal title='编辑设备名称' :show="updateShow" showCancelButton @confirm='update(selectItem.id)'
				@cancel='updateShow=false'>
				<u-input placeholder="请输入设备名称" v-model="selectItem.name" :disabled="disabled" />
			</u-modal>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: [],
				loading: true,
				selectShow: false,
				selectItem: {},
				delShow: false,
				disabled: false,
				updateShow: false,
				columns: [{
						name: '编辑'
					},
					{
						name: '删除'
					}
				],
			}
		},
		onLoad() {


		},
		methods: {
			update(id) {
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '保存中...'
				})
				this.$api_company.Device.Update(id, {
					serial_number: this.selectItem.serial_number,
					name: this.selectItem.name
				}).then(err => {
					this.data = this.data.map(item => {
						if (item.id != id) return item
						return {
							...this.selectItem
						}
					})
					this.disabled = false
					this.updateShow = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
					this.disabled = false
				})
			},
			del(id) {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api_company.Device.Del(id, {
					serial_number: this.selectItem.serial_number
				}).then(err => {
					this.data = this.data.filter(item => item.id != id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '编辑') {
					this.updateShow = true
				} else if (e.name == '删除') {
					this.delShow = true
				}
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = {
					...item
				}
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/camera/add'
				})
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api_company.Device.List(obj).then(res => {
					this.$refs.paging.complete(res.data.devices.data)
					this.loading = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			}
		}
	}
</script>

<style lang="scss">
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.add {
		position: fixed;
		right: 5vw;
		bottom: 20vh;
		border-radius: 100%;
		box-shadow: 0 0 10rpx #ddd;
		background: rgba(0, 0, 0, 0.7);
		padding: 20rpx;

	}

	.BodyBox {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		align-items: flex-start;
	}


	.bodyHeader {
		display: flex;
		justify-content: space-between;
		align-items: center;

		>view:nth-child(2) {
			flex: 1;
			margin-left: 10rpx;
		}
	}

	.bodyItem {
		padding: 20rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		flex-direction: column;
		width: 49%;
		border-radius: 8rpx;
		background-color: #eee;
		height: 200rpx;
		margin-top: 20rpx;
	}

	.font40 {
		font-size: 40rpx;
	}

	.icon-guanji- {
		color: #bbb;
	}
</style>